﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace common
{
    public class ExportQECode
    {
        public void ExportToWord(System.Windows.Forms.DataGridView agrid)
        {
            Object none = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            wordApp.Visible = false;
            Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);

            System.Windows.Forms.ProgressBar abar = new System.Windows.Forms.ProgressBar();
            abar.Width = agrid.Width / 3;
            abar.Height = 16;
            abar.Minimum = 0;
            abar.Maximum = agrid.RowCount;
            abar.Step = 1;
            abar.Left = (agrid.Width - abar.Width) / 2;
            abar.Top = (agrid.Height - abar.Height) / 2;
            abar.Value = 0;

            abar.Parent = agrid;
            agrid.Controls.Add(abar);

            abar.Visible = true;

            System.Windows.Forms.Label alb = new System.Windows.Forms.Label();
            alb.Parent = abar;
            abar.Controls.Add(alb);
            alb.Left = abar.Width / 2;
            alb.Height = 10;
            alb.Top = 3;
            alb.AutoSize = true;
            alb.Text = "0%";
            alb.BackColor = System.Drawing.Color.FromArgb(100, abar.ForeColor);

            //建立表格
            Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, agrid.Rows.Count * 4, 2, ref none, ref none);
            //设置边框
            table.Borders.Enable = 1;
            try
            {
                //for (int row = 1; row < agrid.Rows.Count; row++)
                //{
                //    //设置标题
                //    for (int column = 0; column < agrid.Columns.Count; column++)
                //    {
                //        table.Cell(column + 1, 1).Range.Text = agrid.Columns[column].HeaderText;
                //    }
                //    //填充数据
                //    for (int column = 0; column < agrid.Columns.Count; column++)
                //    {
                //        table.Cell(column + 1, 2).Range.Text = (agrid[column, row - 1].Value == null) ? "" : agrid[column, row - 1].Value.ToString();
                //    }
                //    abar.Value = abar.Value + 1;
                //    alb.Text = abar.Value * 100 / abar.Maximum + "%";
                //}
                for (int row = 0; row < agrid.Rows.Count; row++)
                {
                    table.Cell(row * 4 + 1, 1).Range.Text = agrid.Columns["Cardnum"].HeaderText;
                    table.Cell(row * 4 + 2, 1).Range.Text = agrid.Columns["Cardtype"].HeaderText;
                    table.Cell(row * 4 + 3, 1).Range.Text = agrid.Columns["Qrnum"].HeaderText;
                    table.Cell(row * 4 + 4, 1).Range.Text = "二维码";

                    table.Cell(row * 4 + 1, 2).Range.Text = (agrid["Cardnum", row].Value == null) ? "" : agrid["Cardnum", row].Value.ToString();
                    table.Cell(row * 4 + 2, 2).Range.Text = (agrid["Cardtype", row].Value == null) ? "" : agrid["Cardtype", row].Value.ToString();
                    table.Cell(row * 4 + 3, 2).Range.Text = (agrid["Qrnum", row].Value == null) ? "" : agrid["Qrnum", row].Value.ToString();
                    table.Cell(row * 4 + 4, 2).Range.Select();
                    string filename = System.Windows.Forms.Application.StartupPath + @"\tmp\" + agrid["Cardnum", row].Value.ToString() + ".jpg";
                    
                    object Anchor = document.Application.Selection.Range;
                    AddPic.AddSimplePic(document, filename, 117, 117, Anchor);

                    abar.Value = abar.Value + 1;
                    alb.Text = abar.Value * 100 / abar.Maximum + "%";
                }
                abar.Visible = false;
                abar.Value = 0;

                wordApp.Visible = true;
                //wordApp.UserControl = true;

            }
            catch (Exception theException)
            {
                String errorMessage;
                errorMessage = "Error: ";
                errorMessage = String.Concat(errorMessage, theException.Message);
                errorMessage = String.Concat(errorMessage, " Line: ");
                errorMessage = String.Concat(errorMessage, theException.Source);

                MessageBox.Show(errorMessage, "Error");
            }
        }
    }

    public class AddPic
    {
        public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An)
        {
            //插入图片
            string FileName = @FName;//图片所在路径
            object LinkToFile = false;
            object SaveWithDocument = true;
            object Anchor = An;
            WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
            WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//图片宽度
            WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//图片高度
        }

    }
}
